import {
  SHOW_SUCCESS_MSG,
  SHOW_ERROR_MSG,
  SET_RESULTS
} from '../mutation-types'
import {
  createVoteResult,
  getVoteResultById,

} from '@/api/result';

const state = {
  voteResultList: []
}

const getters = {}

const actions = {
  //请求创建投票结果
  reqCreateVoteResult({
    commit,
    state
  }, payload) {
    return new Promise((resolve, reject) => {
      createVoteResult(payload).then(response => {
        let {
          data: result
        } = response;
        if (result.states == 200) {
          commit(SHOW_SUCCESS_MSG, {
            time: 0,
            needRedirect: true,
            redirectPathName: 'Success',
            messagecode: `post-vote-${result.states}`
          });
        } else {
          commit(SHOW_ERROR_MSG, {
            // messagecode: `err-service-unknow-500`
            messagecode: `post-vote-${result.states}`
          });
        }
        resolve(response);
      }).catch(error => {
        reject(error)
      });
    });
  },
  //请求获取投票结果
  reqGetVoteResultById({
    commit,
    state
  }, payload) {
    return new Promise((resolve, reject) => {
      getVoteResultById(payload).then(response => {
        let {
          data: result
        } = response;
        console.log(result);
        commit(SET_RESULTS, result);
        resolve(response);
      }).catch(error => {
        reject(error)
      });
    });
  }
}

const mutations = {
  [SET_RESULTS](state, results) {
    state.voteResultList = results.results;
  },
}

export default {
  state,
  getters,
  actions,
  mutations
}
